package org.example.netty.dev1.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.example.netty.dev1.entity.Request;
import org.example.netty.dev1.entity.Response;

import java.util.UUID;

public class NettyClientHandler extends SimpleChannelInboundHandler<Response> {


    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("客户端与服务端通道-开启：" + ctx.channel().localAddress() + "channelActive");
//        Request request = new Request();
//        request.setRequestId(UUID.randomUUID().toString());
//        request.setParameter("hello server !");
//        ctx.writeAndFlush(request);

    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("客户端与服务端通道-关闭：" + ctx.channel().localAddress() + "channelInactive");
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Response msg) throws Exception {
        System.out.println(">>>>>>>>>>>>response="+msg.toString());
    }
}
